package com.czz.manage.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.Course;
import com.czz.manage.query.AdminCourseQuery;
import com.czz.manage.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @ClassName: CourseAdminController
 * @Author Czz
 * @Date 2020/2/23
 * @Time 15:36
 **/
@RestController
@RequestMapping(value = "/admin/courses")
public class CourseAdminController {

    @Autowired
    CourseService courseService;

    /**
    * @Description 分页查询所有课程
    * @Param [page, limit, courseQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/23
    * @Time 15:44
    **/
    @GetMapping(value = "/{page}/{limit}")
    public R getCoursePageList(@PathVariable Long page, @PathVariable Long limit, AdminCourseQuery courseQuery){
        if (page <= 0 || limit <= 0){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<Course> pageParam = new Page<>(page, limit);
        pageParam.setRecords(courseService.getAdminCoursePageList(pageParam,courseQuery));
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", pageParam.getRecords());
    }

    /**
    * @Description 上/下架课程
    * @Param [id, status]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/23
    * @Time 16:50
    **/
    @PutMapping(value = "/{id}/{status}")
    public R updateCourseStatus(@PathVariable String id,@PathVariable Integer status){
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(status)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        courseService.updateCourseStatus(id,status);
        return R.ok();
    }

    /**
    * @Description 根据课程名模糊搜索课程
    * @Param [name]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/3/13
    * @Time 11:05
    **/
    @GetMapping(value = "/{name}")
    public R getCourseByLikeName(@PathVariable String name){
        if (StringUtils.isEmpty(name)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        List<Map<String,String>> mapList = courseService.getCourseByLikeName(name);
        return R.ok().data("item",mapList);
    }

    /**
    * @Description 根据课程id查询课程名称
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/3/13
    * @Time 11:46
    **/
    @GetMapping(value = "/name/{id}")
    public R getCourseNameById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Course course = courseService.getById(id);
        System.out.println(course);
        return R.ok().data("item",course);
    }
}
